import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '@/store/index'

Vue.use(VueRouter)

const routes = [

  {
    path: "*",
    redirect: "/404", // 404页面匹配
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/404.vue"),
    meta: {
      title: "404",
      isLogin: false,
      alive: true,
    },
    hidden: true,
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    redirect: "/main",
    meta: {
      requireLogin: true,
    },

  },
  //审批任务列表页面
  {
    path: "/main",
    name: "main",
    component: () => import("@/views/main/mainList.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/community",
    name: "community",
    component: () => import("@/views/main/community.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/streetShops",
    name: "streetShops",
    component: () => import("@/views/main/streetShops.vue"),
    meta: {
      requireLogin: false,
      keepAlive: false
    },
  },
  {
    path: "/shopsDetail",
    name: "shopsDetail",
    component: () => import("@/views/main/shopsDetail.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/checkPage",
    name: "checkPage",
    component: () => import("@/views/main/checkPage.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/report",
    name: "report",
    component: () => import("@/views/main/report.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/productMsg",
    name: "productMsg",
    component: () => import("@/views/main/productMsg.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/checkRecord",
    name: "checkRecord",
    component: () => import("@/views/main/checkRecord.vue"),
    meta: {
      requireLogin: false,
    },
  },
  {
    path: "/businessList",
    name: "businessList",
    component: () => import("@/views/main/businessList.vue"),
    meta: {
      requireLogin: false,
    },
  },
];

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {

  if (to.meta.requireLogin) {
    if (store.getters['isLogin']) {
      next();
    } else {
      next();
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      });
    }
  } else {
    next();
  }
});

export default router
